﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;


namespace WebComponents.Templates.AdminControlProj
{
    #region Control
    [
        //AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
        //AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    Designer(typeof(LayoutDesigner)),
    DefaultProperty("Title"),
    ToolboxBitmap(typeof(Layout), "WebComponents.Templates.AdminControlProj.Images.iconLayout.bmp"),
    ToolboxData("<{0}:Layout runat=\"server\"> </{0}:Layout>"),
    ]
    public class Layout : CompositeControl
    {
        private string title;
        private ITemplate content;
        private LayoutContent contentValue;
        /// <summary>
        /// 
        /// </summary>
        public virtual string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(LayoutType.Main),
        Description("Type of layout"),
        ]
        public virtual LayoutType LayoutType
        {
            get
            {
                object t = ViewState["LayoutType"];
                return (t == null) ? LayoutType.Main : (LayoutType)t;
            }
            set
            {
                ViewState["LayoutType"] = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>

        [
        Browsable(false),
        DesignerSerializationVisibility(
            DesignerSerializationVisibility.Hidden)
        ]
        public LayoutContent ContentValue
        {
            get
            {
                return contentValue;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [
        Browsable(false),
        PersistenceMode(PersistenceMode.InnerProperty),
        DefaultValue(typeof(ITemplate), ""),
        Description("Control Content"),
        TemplateContainer(typeof(Layout)),
        ]
        public virtual ITemplate Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }


        protected override void CreateChildControls()
        {
            Controls.Clear();

            #region content
            this.contentValue = new LayoutContent();

            ITemplate _content = content;
            if (_content == null)
            {
                _content = new LayoutTemplate();
            }
            _content.InstantiateIn(this.contentValue);

            this.Controls.Add(this.contentValue);
            #endregion

            #region title

            #endregion
        }


        protected override void RenderContents(HtmlTextWriter writer)
        {
            contentValue = new LayoutContent();
            ITemplate _content = content;
            if (_content == null)
            {
                _content = new LayoutTemplate();
            }
            _content.InstantiateIn(contentValue);

            AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "1", false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            if (Title != null)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                (new LiteralControl(title)).RenderControl(writer);
                (new LiteralControl("<hr />")).RenderControl(writer);
                contentValue.RenderControl(writer);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }
    }

    [ToolboxItem(false)]
    public class LayoutContent : WebControl
    {
    }
    #endregion

    #region Template

    public enum LayoutType
    {
        Simple = 0,
        Main = 1
    }

    sealed class LayoutTemplate : ITemplate
    {
        void ITemplate.InstantiateIn(Control content)
        {
            LiteralControl lctrl = new LiteralControl("empty");
            content.Controls.Add(lctrl);
        }

    }
    #endregion

    #region Designer
    public class LayoutDesigner : ControlDesigner
    {

        public override void Initialize(IComponent Component)
        {
            base.Initialize(Component);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        public override string GetDesignTimeHtml()
        {
            return "<span>This is design-time HTML</span>";
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection collection = new TemplateGroupCollection();
                TemplateGroup group;
                TemplateDefinition template;
                Layout control;

                control = (Layout)Component;
                group = new TemplateGroup("Item");
                template = new TemplateDefinition(this, "Template", control, "Template", true);
                group.AddTemplateDefinition(template);
                collection.Add(group);
                return collection;
            }
        }
    }
    #endregion
}


